package com.ajie.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONObject;
import com.ajie.entity.SysPermission;
import com.ajie.entity.SysUser;
import com.ajie.entity.dto.Option;
import com.ajie.mapper.SysUserMapper;
import com.ajie.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: ajie
 * @createTime: 2024-06-22 18:10
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Override
    public List<Option> getRoles(String id) {
        return baseMapper.getRolesByUserId(id);
    }

    @Override
    public void insertRole(String id, List<Object> roleIds) {
        baseMapper.insertRoleIds(id, roleIds);
    }

    @Override
    public void deleteRole(String id) {
        baseMapper.deleteRoleByUserId(id);
    }

    @Override
    public List<String> listRoleByUserId(String userId) {
        return baseMapper.getRoleCodeByUserId(userId);
    }

    @Override
    public List<String> listPermissionByRoleCode(String roleCode) {
        return baseMapper.listPermissionByRoleCode(roleCode);
    }

    @Override
    public List<JSONObject> listMenu() {
        List<JSONObject> data = new ArrayList<>();
        // 先获取父级菜单
        List<SysPermission> list = baseMapper.listParentMenuByUserId(StpUtil.getLoginIdAsString());
        for (SysPermission permission : list) {
            JSONObject parentMenu = new JSONObject();
            parentMenu.putOnce("path", permission.getPath());
            parentMenu.putOnce("component", "LAYOUT");
            JSONObject parenMata = new JSONObject();
            getMenuTitle(permission, parentMenu, parenMata);
            List<JSONObject> children = new ArrayList<>();
            // 获取子级菜单
            List<SysPermission> childrenList = baseMapper.listMenuByUserId(StpUtil.getLoginIdAsString(), permission.getId());
            // 迭代子菜单构造
            for (SysPermission menuInfo : childrenList) {
                JSONObject menu = new JSONObject();
                menu.putOnce("path", menuInfo.getPath());
                menu.putOnce("name", menuInfo.getPath());
                JSONObject mata = new JSONObject();
                if (menuInfo.getOuterChain() == 1) {
                    menu.putOnce("component", "IFrame");
                    mata.putOnce("frameSrc", menuInfo.getCode());
                } else {
                    menu.putOnce("component", menuInfo.getCode());
                }
                getMenuTitle(menuInfo, menu, mata);
                children.add(menu);
            }
            parentMenu.putOnce("children", children);
            data.add(parentMenu);
        }
        return data;
    }

    private void getMenuTitle(SysPermission menuInfo, JSONObject menu, JSONObject mata) {
        JSONObject title = new JSONObject();
        title.putOnce("zh_CN", menuInfo.getName());
        title.putOnce("en_US", menuInfo.getEnglishName());
        mata.putOnce("title", title);
        mata.putOnce("icon", menuInfo.getIcon());
        menu.putOnce("meta", mata);
    }
}
